package swim2.sessionbeans;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;


@Stateless
@RemoteBinding(jndiBinding="cleanJNDI")
public class CleanDatabaseSystem implements CleanDatabaseSystemRemote{
	
	@PersistenceContext(unitName="swim2") private EntityManager manager;

	public void clearUser() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from User");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	
	public void clearAbility() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from Ability");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	
	public void clearUserAbility() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from UserAbility");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	
	public void clearWorkRequest() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from WorkRequest");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	
	public void clearAbilityRequest() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from AbilityRequest");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	

	public void clearFeedback() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from Feedback");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	
	public void clearWorkers() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from Workers");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	
	public void clearFriendship() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from Friendship");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	
	public void clearFriendRequest() {
		Query q = manager.createNativeQuery("set foreign_key_checks = 0");
		q.executeUpdate();
		
		q = manager.createNativeQuery("delete from FriendRequest");
		q.executeUpdate();
		
		
		q = manager.createNativeQuery("set foreign_key_checks = 1");
		q.executeUpdate();		
	}
	
	public void clearDatabase() {	
		clearFriendRequest();
		clearUser();
		clearAbility();
		clearUserAbility();
		clearAbilityRequest();
		clearWorkers();
		clearWorkRequest();
		clearFriendship();
		clearFeedback();
		
	}	
}
